//全局变量
var g_sumPage; //总页数

$(function () {

    $.post("/book/catalogue/classification", {}, function (result) {
        var data = $.parseJSON(result);
        var colorNum = 0;
        var colors = new Array();
        colors[0] = "label-color1";
        colors[1] = "label-color2";
        colors[2] = "label-color3";
        colors[3] = "label-color4";
        colors[4] = "label-color5";
        colors[5] = "label-color6";
        colors[6] = "label-color7";
        colors[7] = "label-color8";

        $.each(data, function (key, value) {
            var tempHtml = [];
            var classificationHtml = "";
            if (colorNum > colors.length - 1) {
                colorNum = 0;
            }
            tempHtml.push('<a href="#"><span class="label ' + colors[colorNum] + '">');
            tempHtml.push(key + " #" + value);
            tempHtml.push('</span></a>&nbsp;');
            classificationHtml = tempHtml.join("");
            $("h2.t-button").append(classificationHtml);

            colorNum++;
        });
    });

    //$.post("/book/catalogue/allbook", {}, function (result) {
    //    var data = $.parseJSON(result);
    //    if ($("tbody tr").length > 0) {
    //
    //    } else {
    //        $.each(data, function (index, obj) {
    //            var tempHtml = [];
    //            var recordHtml;
    //            index = index + 1;
    //            tempHtml.push("<tr>");
    //            tempHtml.push("<td>" + index + "</td>");
    //            tempHtml.push("<td>" + obj.bookIsbn + "</td>");
    //            tempHtml.push("<td>" + obj.bookName + "</td>");
    //            tempHtml.push("<td>" + obj.bookAuthor + "</td>");
    //            tempHtml.push("<td>" + obj.bookLanguage + "</td>");
    //            tempHtml.push("<td>" + obj.bookPublisher + "</td>");
    //            tempHtml.push("<td>" + obj.bookClassification + "</td>");
    //            tempHtml.push("</tr>");
    //            recordHtml = tempHtml.join("");
    //            $("#bookcatalogue_tbody").append(recordHtml);
    //        });
    //    }
    //});



});


// ========================================================== 分页代码
$(document).ready(function () {
    $.ajax({
        url: "/book/catalogue/sumpage.json",
        async: false,
        type: "POST",
        data: {},
        success: function(result){
            g_sumPage = result;
        }
    })
    loadPageData(1);
    loadPageNumber(1);


});

$( document ).on( "click", "#page>li>a", function() {
        var pageNum = $(this).text();
    loadPageData(pageNum);
    $("#page>li").removeClass("active");
    $(this).parent().attr("class", "active");
    $("#page>li>a>span").remove();
    $(this).append('<span class="sr-only">(current)</span>');
});

$("#pre>li>a").click(function(){
   if (!$("#pre>li:first").hasClass("disabled")){ //不是第一页,可以翻页
       //翻到前一页
       var start = parseInt($("#page>li:first>a").text());
       loadPageNumber(start-10);
       if($("#page>li:first>a").text() == 1){ //翻到了第一页
           $("#pre>li:first").addClass("disabled");
       }
       if($("#next>li:first").hasClass("disabled")){
           $("#next>li:first").removeClass("disabled");
       }

       $("#page>li:first>a").click();
   }
});

$("#next>li>a").click(function(){
    if (!$("#next>li:first").hasClass("disabled")){ //不是最后页,可以翻页
        //翻到前一页
        var end = parseInt($("#page>li:last>a").text());
        loadPageNumber(end+1);
        if($("#page>li:last>a").text() == g_sumPage){ //翻到了last页
            $("#next>li:first").addClass("disabled");
        }
        if($("#pre>li:first").hasClass("disabled")){
            $("#pre>li:first").removeClass("disabled");
        }

        $("#page>li:first>a").click();
    }
});
// ========================================================== 分页代码完

function loadPageData(argPage) {
    var page = argPage;
    $("#bookcatalogue_tbody").empty();
    $.post
    ("/book/catalogue/page", {
        pageNum: page
    }, function (result) {
        var data = $.parseJSON(result);
        $.each(data, function (index, obj) {
            var tempHtml = [];
            var recordHtml;
            index = index + 1 + (page - 1) * 10;
            tempHtml.push("<tr>");
            tempHtml.push("<td>" + index + "</td>");
            tempHtml.push("<td>" + obj.bookIsbn + "</td>");
            tempHtml.push("<td>" + obj.bookName + "</td>");
            tempHtml.push("<td>" + obj.bookAuthor + "</td>");
            tempHtml.push("<td>" + obj.bookLanguage + "</td>");
            tempHtml.push("<td>" + obj.bookPublisher + "</td>");
            tempHtml.push("<td>" + obj.bookClassification + "</td>");
            tempHtml.push("</tr>");
            recordHtml = tempHtml.join("");
            $("#bookcatalogue_tbody").append(recordHtml);
        });
    });
}

function loadPageNumber(start){ //start是页码
    $("#page>li").remove();
    var count = g_sumPage - start;
    count = (count>9)?10:count+1;
    for(var i=0;i<count;i++){
        var htmlArray =[];
        htmlArray.push('<li><a href="javascript:void(0)">');
        htmlArray.push(start+i);
        htmlArray.push('</a></li>');
        $("#page").append(htmlArray.join(""));
    }
    if(start == 1){
        $("#pre>li:first").addClass("disabled");

    }
}